package com.strong.faker;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import com.strong.enum_.EmailDomainEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * 手机 模拟数据生成类
 *
 * @author Administrator
 * @date 2023/04/08
 */
@Slf4j
public class EmailFaker extends BaseFaker<String> {

    /**
     * 邮件域名枚举数组
     */
    private EmailDomainEnum[] emailDomainEnums;

    /**
     * 邮件域名段数组
     */
    private String[] strsDomain;

    /**
     * 是否初始化
     */
    private boolean isInitialized = false;

    /**
     * 注入邮件域名
     *
     * @param emailDomainEnums 邮箱域名枚举数组
     * @return {@link EmailFaker}
     */
    public EmailFaker domain(EmailDomainEnum... emailDomainEnums) {
        this.emailDomainEnums = emailDomainEnums;
        this.isInitialized = false;
        return this;
    }

    @Override
    protected void initialize() {
        // 如果枚举数组为空，则默认所有枚举
        if (ArrayUtil.isEmpty(this.emailDomainEnums)) {
            this.emailDomainEnums = EmailDomainEnum.values();
        }

        // 如果域名数组为空，则初始化数组
        if (!this.isInitialized || ArrayUtil.isEmpty(this.strsDomain)) {
            this.strsDomain = ArrayUtil.addAll(Arrays.stream(this.emailDomainEnums).map(EmailDomainEnum::getDomain).toArray(String[][]::new));
            this.isInitialized = true;
        }

        log.info("【EMailFaker】邮箱域名参数：{}", ArrayUtil.toString(this.strsDomain));
    }

    @Override
    protected String getModel() {
        String strPrefix = RandomUtil.randomString(RandomUtil.randomInt(8));
        return "%s@%s".formatted(strPrefix, RandomUtil.randomEle(this.strsDomain));
    }
}
